package net.seehealth.healthpad.ui;

import net.seehealth.healthpad.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.PopupWindow;
import android.widget.Toast;

public class BaseActivity extends Activity {

	/** 当前上下文 **/
	protected Context mContext;
	
	/** 弹出框 **/
	protected ProgressDialog mProgressDialog;
	protected PopupWindow mPopupwindow;
	protected View vPopupWindow;

	/** 存储数据 **/
	protected SharedPreferences sp; 

	/** 当前屏幕宽度 **/
	protected int mScreenW;

	/** 当前屏幕高度 **/
	protected int mScreenH;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		
	}

	@Override
	protected void onPause() {
		super.onPause();
		
	}

	@Override
	protected void onResume() {
		super.onResume();
		
	}

	/**
	 * Toast显示
	 * @param text 显示的内容
	 * @param context 当前上下文
	 */
	public void showToast(CharSequence text, Context context) {
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}

	/**
	 * Toast显示
	 * @param text 显示的内容
	 * @param context  当前上下文
	 */
	protected void showToast(String text, Context context) {
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}

	/**
	 * Toast显示
	 * @param resId 提示信息资源ID
	 * @param context 当前上下文
	 */
	public void showToast(int resId, Context context) {
		Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
	}

	/**
	 * Toast显示
	 * @param resId 提示信息资源ID
	 * @param context 当前上下文
	 */
	protected void showToast(int resId, Context context, int length) {
		Toast.makeText(context, context.getResources().getString(resId), length).show();
	}
	
	
	/**
	 * 得到一个进度条
	 * @param message 显示的内容
	 * @param context 当前上下文
	 * @return
	 */
	public ProgressDialog getProgressDialog(String message, Context context, AsyncTask<?, ?, ?> task, boolean isDismiss) {
		ProgressDialog progressDialog = new ProgressDialog(context);
		progressDialog.setMessage(message);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(true);
		mProgressDialog = progressDialog;
		return mProgressDialog;
	}

	/**
	 * 设置消息
	 * @param message 提示消息
	 */
	public void setMessage(String message, AsyncTask<?, ?, ?> task, boolean isDismiss) {
		mProgressDialog.setMessage(message);
	}

	public void setMessage(int message, AsyncTask<?, ?, ?> task, boolean isDismiss) {
		mProgressDialog.setMessage(getString(message));
	}
	
	/**
	 * 显示进度条
	 * @param messageId 显示的内容
	 * @param context 当前上下文
	 * @param task 
	 * @param isDismiss 是否按结束此异步类 true：取消 false：不取消
	 */
	public void showProgressDialog(String message, Context context, AsyncTask<?, ?, ?> task, boolean isDismiss) {
		getProgressDialog(message, context, task, isDismiss).show();
	}
	
	/**
	 * 显示进度条
	 * @param messageId 显示的内容ID
	 * @param context 当前上下文
	 * @param task 
	 * @param isDismiss 是否按结束此异步类 true：取消 false：不取消
	 */
	public void showProgressDialog(int messageId, Context context, AsyncTask<?, ?, ?> task, boolean isDismiss) {
		getProgressDialog(messageId, context,task, isDismiss).show();
	}


	/**
	 * 得到一个进度条
	 * @param messageId 显示的内容ID
	 * @param context 当前上下文
	 * @param task 
	 * @param isDismiss 是否按结束此异步类 true：取消 false：不取消
	 * @return
	 */
	public ProgressDialog getProgressDialog(int messageId, Context context, AsyncTask<?, ?, ?> task, boolean isDismiss) {
		return getProgressDialog(getString(messageId), context, task, isDismiss);
	}
	
	/**
	 * 隐藏对话框
	 */
	public void closeProgressDialog() {
		try { 
			if(null != mProgressDialog && mProgressDialog.isShowing()) {
				mProgressDialog.dismiss();
				mProgressDialog = null;
			}
		} catch (Exception e) {   
		} 
	}
	
	
	/**
	 * 显示pop
	 * @param ResId
	 * @param v
	 * @param xoff
	 * @param yoff
	 * @param isCenter 是否居中
	 */
	protected void showPopWindow(int ResId,View v,int xoff,int yoff,boolean isCenter){
		LayoutInflater inflater = (LayoutInflater) mContext
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		vPopupWindow = inflater.inflate(ResId, null,
				true);
		mPopupwindow = new PopupWindow(vPopupWindow, ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		mPopupwindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupwindow.setFocusable(true);
		if(!isCenter){
			//mPopupwindow.showAsDropDown(v);
			mPopupwindow.showAsDropDown(v,xoff,yoff);
		}else{
			mPopupwindow.showAtLocation(v, Gravity.CENTER, 0, 0);
		}
	}
	
	protected String getStringData(String count,String color){
		return "<font color="+color+">" + count + "</font>";
	}

	/**
	 * 动画显示Pop
	 */
	protected void showPopWindowAmini(int ResId,View v,int xoff,int yoff){
		LayoutInflater inflater = (LayoutInflater) mContext
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		vPopupWindow = inflater.inflate(ResId, null,
				true);
		mPopupwindow = new PopupWindow(vPopupWindow, LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);

		mPopupwindow.setAnimationStyle(R.style.mypopwindow_anim_style); //设置动画
		mPopupwindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupwindow.setFocusable(true);
		mPopupwindow.showAtLocation(v, Gravity.CENTER, xoff, yoff);     //居中来显示
		mPopupwindow.update();
	}

	/**
	 * 隐藏popWindow
	 */
	protected void closePopWindow() {
		if(null != mPopupwindow && mPopupwindow.isShowing()) {
			mPopupwindow.dismiss();
		}
	}

	/**
	 * 检测操作状态，并给出提示
	 * @return false:返回 true:继续操作
	 */
	protected boolean checkLoginState() {
		
		return false;
	}

}

	